"""
Case Type   : WEAK_PASSWORD_DICTIONARY
Case Name   : 普通用户删除插入的弱口令,合理报错
Create At   : 2022/08/12
Owner       : @zou_jialiang050
Description :
    1.创建用户
    2.向系统表gs_global_config添加弱口令
    3.查看系统表
    4.普通用户删除插入的弱口令
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.返回新插入弱口令
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Confirguration(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user = 'u_alter_global_configuration_0020'

    def test_alter_global_configuration(self):
        step = 'step1:创建用户 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user} cascade;
            create user {self.user} with password '{macro.PASSWD_REPLACE}';''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:向系统表gs_global_config插入弱口令 expect:成功'
        self.logger.info(step)
        alter_configuration = self.primary_sh.execut_db_sql(
            f'''create weak password dictionary ('test_123A'),
            ('test1_234W');''')
        self.logger.info(alter_configuration)
        self.assertIn('CREATE WEAK PASSWORD DICTIONARY', alter_configuration,
                      '执行失败' + step)

        step = 'step3:查看系统表 expect:返回新插入弱口令'
        self.logger.info(step)
        select_configuration = self.primary_sh.execut_db_sql(
            f'''select * from gs_global_config;''')
        self.logger.info(select_configuration)
        self.assertIn('test_123A', select_configuration, '执行失败' + step)
        self.assertIn('test1_234W', select_configuration, '执行失败' + step)

        step = 'step4:普通用户删除插入的弱口令 expect:合理报错'
        self.logger.info(step)
        clean_configuration = self.primary_sh.execut_db_sql(
            f'''drop weak password dictionary;''',
            sql_type=f" -U {self.user} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(clean_configuration)
        self.assertIn('ERROR:  Permission denied', clean_configuration,
                      '执行失败' + step)

    def tearDown(self):
        step = 'step5:清理环境 expect:成功'
        self.logger.info(step)
        clean_enviro = self.primary_sh.execut_db_sql(
            f'''drop weak password dictionary;
            drop user {self.user};''')
        self.logger.info(clean_enviro)
        self.assertIn('DROP WEAK PASSWORD DICTIONARY', clean_enviro,
                      '执行失败' + step)
        self.assertIn(self.Con.DROP_ROLE_SUCCESS_MSG, clean_enviro,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
